import {
    request,
    WXNavigateTo,
    WXshowToast,
    WXReLaunch
} from '../../utils/index.js';
import { Api } from '../../config/api.js';
const appInst = getApp();   
const { imgBaseUrl, host, color } = appInst.globalData;

Page({
  data: {
    imgBaseUrl: appInst.globalData.imgBaseUrl,
    addressList: [],
    defaultAddrId:null,
    host
  },

   onLoad: function (options) {
      let goodid = options.goodid
      let price = options.price
      this.setData({
          goodid,
          price
      })
   },

  onShow: function () {
    this._getAddressList();
  },


 //   获取收货地址
async  _getAddressList() {
      const {data} = await request({
          url: Api.getAddressList,
            method: 'POST'
      })
    this.setData({
        addressList: data.data,
    })
  },



//   单选按钮

  radioChange(e) {
        const {addressList} = this.data
        const items = addressList
          for (let i = 0, len = items.length; i < len; i++) {
              items[i].state = i === Number(e.detail.value)
          }
        this.setData({
            addressList:items,
            defaultAddrId: Number(e.detail.value)
        })

  },

//   删除收货地址
   async del(e){    
          const {id} =e.currentTarget.dataset
          const {data} = await request({
              url: Api.delAddress,
              method:"POST",
              data:{id}
          })
          if(data.code==2001){
              this._getAddressList()
          }
    },

  async tacitly(e){
    const {id} =e.currentTarget.dataset
          const {data} = await request({
              url: Api.tacitly,
              method:"POST",
              data:{id}
          })
          if(data.code==2001){
              // this._getAddressList()
          }
  },

  async dz(e){
    const {index} = e.currentTarget.dataset //获取地址下标 
    const {addressList} = this.data //所有地址列表
    if(addressList){
      let data = addressList[index]
      wx.setStorageSync(
        'dzactive',data

      )
      console.log(data)
        if (this.data.goodid) {
            wx.showModal({
                title: '提示',
                content: '请确定收货地址',
                success:res=> {
                    if (res.confirm) {
                        WXNavigateTo(`/pages/LipinCard/LipinCard?goodid=${this.data.goodid}&price=${this.data.price}&address_id=${data.id}`)
                    } 
                }
            })
        }else{
             wx.navigateBack({ //返回
                 delta: 1
             })
        }
    }
  }
})